<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信企业号-应用消息推送</title>
    <link rel="stylesheet" href="/css/zui.min.css">
</head>
<body>
[#if status??]
<div>
    <h4>${error_message}</h4>
</div>
[#else]
<div class="container-fixed-md">
    <ol class="breadcrumb" style="margin-top: 15px;margin-bottom: 0px;">
        <li><a href="/"><i class="icon icon-home"></i> 首页</a></li>
        <li class="active">定时任务</li>
        <div class="pull-right text-primary">
            <a href="/agent-message?agent_id=${agentId}"><i class="icon icon-plus"></i> 新建定时任务</a>
        </div>
    </ol>
    <hr style="margin-top: 0px; margin-bottom: 0px;">
</div>

<div class="container-fixed-md">

    <table class="table table-striped table-hover">
        <thead>
        <tr>
            <th class="text-center"> #</th>
            <th>任务编号</th>
            <th>任务描述</th>
            <th>Cron</th>
            <th>开始时间</th>
            <th>结束时间</th>
            <th class="text-center">运行状态</th>
            <th class="text-center">任务操作</th>
        </tr>
        </thead>
        <tbody>
        [#list scheduleJobs as job]
        <tr>
            <td>${job.id}</td>
            <td>${job.getJobKey()}</td>
            <td>${(job.description!'')[0..*20]}</td>
            <td>${job.cronExpression}</td>
            <td>${(job.startDate?string("yyyy-MM-dd HH:mm:ss"))!""}</td>
            <td>${(job.endDate?string("yyyy-MM-dd HH:mm:ss"))!""}</td>
            <td class="text-center">
                [#if currentStatus[job.getJobKey()]]
                <i class="icon icon-play-circle text-green"></i>
                [#else]
                <i class="icon icon-dot-circle text-red"></i>
                [/#if]
            </td>
            <td class="text-center">
                [#if job.status == 1]
                <a href="#" class="option-btn" data-id="${job.id}" data-option="stop" title="禁用"><i class="icon icon-ban-circle text-yellow"></i></a>
                [#else]
                <a href="#" class="option-btn" data-id="${job.id}" data-option="start" title="启用"><i class="icon icon-play text-green"></i></a>
                [/#if]
                <a href="#" class="edit-btn" data-id="${job.id}" data-url="/agent-message?agent_id=${agentId}&schedule_id=${job.id}" title="编辑"><i class="icon icon-pencil text-primary"></i></a>
                <a href="#" class="option-btn" data-id="${job.id}" data-option="delete" title="删除"><i class="icon icon-times text-danger"></i></a>
            </td>
        </tr>
        [/#list]
    </table>

</div>


<script src="/js/jquery1.8.1.js"></script>
<script src="/js/zui.min.js"></script>
<script type="text/javascript">
    var agent_id = '${agentId}';
    var sendMessage = function (job_id,option_type) {
        if (!job_id || !option_type) {
            alert("不能为空");
            return false;
        }
        $.ajax({
            url: '/schedule/state?job_id=' + job_id +'&option_type=' + option_type,
            type: 'PUT',
            dataType: 'json',
            contentType: "application/json",
            // data: msg,
            beforeSend: function () {
                console.log(agent_id + ' start send message');
            },
            success: function (result) {
                console.log(result);
                if (result.status == 'ok') {
                    new $.zui.Messager('  提示消息：成功!', {
                        type: 'success',
                        time: 3000
                    }).show();
                    window.location.reload();
                } else {
                    new $.zui.Messager('  提示消息：' + result.status + '!', {
                        type: 'danger',
                        time: 3000
                    }).show();
                }
            },
            error: function () {
                new $.zui.Messager('  提示消息：请求失败!', {
                    type: 'danger',
                    time: 5000
                }).show();
            },
            complete: function () {

            }
        });
    }

    $('.option-btn').on('click', function () {
        var option_type = $(this).data('option');
        var job_id = $(this).data('id');
        sendMessage(job_id, option_type);
    });

    $('.edit-btn').on('click', function () {
        window.location = $(this).data('url');
    });
</script>
[/#if]
</body>
</html>